<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Satuan_aktifitas extends CI_Controller {

    function __construct() 
	{
        parent::__construct();
        $this->load->library('flexigrid');
        $this->load->library('form_validation');
		$this->load->library('session');
        $this->load->helper('flexigrid');
        $this->load->helper(array('form', 'url'));
        $this->load->model('bagian_jenis_aktifitas_model');
		$this->load->model('aktifitas_model');
		$this->load->model('user_model');
    }
    
	public function index($id_bagian_jenis_aktifitas)
	{
		if ($this->session->userdata('username') == NULL) 
		{
            redirect('login');
        } else {

			$hasil = $this->bagian_jenis_aktifitas_model->selectone($id_bagian_jenis_aktifitas);
			
			foreach ($hasil->result() as $row) 
			{
				$nama_bagian_jenis_aktifitas = $row->nama_bagian_jenis_aktifitas;
			}
		
			$this->session->set_userdata('id_bagian_jenis_aktifitas', $id_bagian_jenis_aktifitas);
			$this->session->set_userdata('nama_bagian_jenis_aktifitas', $nama_bagian_jenis_aktifitas);		  


			$colModel['no'] = array('No', 30, TRUE, 'center', 0);
			$colModel['nama_satuan_jenis_aktifitas'] = array('Satuan Aktifitas', 250, TRUE, 'center', 1);           
			$colModel['Lihat_Detail_Aktifitas'] = array('Lihat Detail Aktifitas', 100, TRUE, 'center', 0);
			$colModel['Plan_Progress'] = array('Plan Progress', 100, TRUE, 'center', 0);
			$colModel['Progress_Periode_1'] = array('Progress Periode 1', 100, TRUE, 'center', 0);			

			//setting konfigurasi pada bottom tool bar flexigrid
			$gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Daftar Satuan Aktifitas</br>Evaluasi : '.$this->session->userdata('NAMA_EVALUASI').'</br>Bagian Jenis Aktifitas : '.$this->session->userdata('nama_bagian_jenis_aktifitas').'',
                'showTableToggleBtn' => true
			);
            									
			//menambah tombol pada flexigrid top toolbar
			
			$buttons[] = array('Perbarui Nilai Progress Aktifitas', 'update', 'spt_js');
            $buttons[] = array('separator');
			$buttons[] = array('Kembali ke Daftar Jenis Aktifitas', 'back', 'spt_js');	
			$buttons[] = array('separator');
						            		
			// mengambil data dari file controler ajax pada method grid_user				
			$grid_js = build_grid_js('flex1', site_url("satuan_aktifitas/grid_foto/".$id_bagian_jenis_aktifitas.""), $colModel, 'nama_satuan_jenis_aktifitas', 'asc', $gridParams, $buttons);

			$data['added_js'] =
                    "<script type='text/javascript'>
			function spt_js(com,grid)
			{				                
                if (com=='Perbarui Nilai Progress Aktifitas')
                {
                        location.href='" . base_url() . "index.php/satuan_aktifitas/form_nilai';
                }
				
				if (com=='Kembali ke Daftar Jenis Aktifitas')
                {
                        location.href='" . base_url() . "index.php/detail_aktifitas/index/".$this->session->userdata('ID_EVALUASI')."';
                }                
			}
			</script>
			";

            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);	
		}
	}
	
    function grid_foto($id_bagian_jenis_aktifitas) 
	{
        $valid_fields = array('nama_satuan_jenis_aktifitas');
        $this->flexigrid->validate_post('nama_satuan_jenis_aktifitas', 'asc', $valid_fields);
		$records = $this->bagian_jenis_aktifitas_model->get_nama_satuan_aktifitas($id_bagian_jenis_aktifitas);
		// $records2 = $this->aktifitas_model->get_total_pv($id_satuan_jenis_aktifitas)->row()->TP1;
			
        $this->output->set_header($this->config->item('json_header'));
        $no = 0;
			
		foreach ($records['records']->result() as $row) 
		{
			$nama_satuan_jenis_aktifitas = $row->nama_satuan_jenis_aktifitas;	
			
			$record_items[] = array
			(
				$row->id_satuan_jenis_aktifitas,
                $no = $no + 1,
				$nama_satuan_jenis_aktifitas,
				'<a href=\'' . base_url() . 'index.php/aktifitas/index/' . $row->id_satuan_jenis_aktifitas . '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/folder.png\'></a> '		
			);
		}

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }	
	
	public function form_nilai()
	{
		$data['status'] = 'new';
		$data['failed'] = false;
		$data['aa'] = '';
		$data['data_form_nilai'] = $this->aktifitas_model->data_form_nilai($this->session->userdata('id_bagian_jenis_aktifitas'), $this->session->userdata('id_satuan_jenis_aktifitas'), $this->session->userdata('ID_EVALUASI'));
		$data['content'] = $this->load->view('form_pk', $data, true);
		$this->load->view('main', $data);
	} 
	
	function save_nilai()
	{
		$data_form_nilai = $this->bagian_jenis_aktifitas_model->data_form_nilai($this->session->userdata('id_bagian_jenis_aktifitas'), $this->session->userdata('id_satuan_jenis_aktifitas'), $this->session->userdata('ID_EVALUASI'));
		
		foreach ($data_form_nilai->result() as $row)
		{
			$id_satuan_jenis_aktifitas = $row->id_satuan_jenis_aktifitas;
			$data = array(
				'id_proses_aktifitas' => $id,
				'id_bagian_jenis_aktifitas' => $this->session->userdata('id_bagian_jenis_aktifitas'),
                'id_satuan_jenis_aktifitas' => $this->session->userdata('id_satuan_jenis_aktifitas'),
				'ID_EVALUASI' => $this->session->userdata('ID_EVALUASI'),			
				'plan_progress' => $this->input->post('plan_progress'.$id_satuan_jenis_aktifitas.''),
				'progress_periode_1' => $this->aktifitas_model->nilai_total_progress($id_satuan_jenis_aktifitas)->row()->TP1			
			);
									
			$this->aktifitas_model->save_nilai($row->id_satuan_jenis_aktifitas, $row->id_bagian_jenis_aktifitas, $data);			
		}
		redirect('satuan_aktifitas/index/'.$this->session->userdata('id_bagian_jenis_aktifitas'));
	}   	
	
}
